package com.ymatou.shop.ui.activity;

import com.ymatou.shop.R;
import com.ymatou.shop.api.ApiRequest;
import com.ymatou.shop.api.ApiResponse;
import com.ymatou.shop.cache.ImageCache;
import com.ymatou.shop.cache.RequestInfo;
import com.ymatou.shop.cache.exception.CacheException;
import com.ymatou.shop.model.LoginResult;
import com.ymatou.shop.model.ResponseMessage;
import com.ymatou.shop.ui.activity.seller.AddProductActivity;
import com.ymatou.shop.ui.activity.seller.CameraActivity;
import com.ymatou.shop.util.DialogUtil;
import com.ymatou.shop.util.GlobalUtil;
import com.ymatou.shop.util.LogUtil;
import com.ymatou.shop.util.StringUtil;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class SendCommentActivity extends BaseHeaderActivity implements OnClickListener {

	private EditText commentEditText;
	private String content, productId;
	public final static String PRODUCTID_KEY = "productid";
	private static final int REQUEST_ID_SEND_COMMENT = 0x01;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.send_comment_activity);

		productId = getIntent().getStringExtra(PRODUCTID_KEY);

		setupViews();

	}

	@Override
	protected void setupViews() {
		super.setupViews();

		commentEditText = (EditText) findViewById(R.id.comment_content);

		getTitleLeftImageButton().setImageResource(R.drawable.back_icon);
		getTitleLeftImageButton().setVisibility(View.VISIBLE);
		getTitleLeftImageButton().setBackgroundDrawable(null);
		getTitleLeftImageButton().setOnClickListener(this);

		getTitleRightButton().setText("发送");
		getTitleRightButton().setOnClickListener(this);
		getTitleRightButton().setVisibility(View.VISIBLE);

	}

	@Override
	public void onCacheSuccess(int requestId, RequestInfo requestInfo, Object response) {
		LogUtil.systemErr((String) response);
		if (requestId == REQUEST_ID_SEND_COMMENT) {
			ResponseMessage mResponseMessage = ApiResponse.sendComment((String) response);
		}

		super.onCacheSuccess(requestId, requestInfo, response);
		finish();
	}

	@Override
	public void onCacheFailed(int requestId, RequestInfo requestInfo, CacheException error) {
		super.onCacheFailed(requestId, requestInfo, error);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_right_button:
			content = commentEditText.getText().toString();
			content = content.trim();
			if (StringUtil.isEmpty(content)) {
				GlobalUtil.shortToast(v.getContext(), "评论不能为空");
				commentEditText.requestFocus();
				return;
			}

			sendComment();

			break;
		default:
			break;
		}

	}

	private void sendComment() {
		if (StringUtil.isEmpty(productId)) {
			GlobalUtil.shortToast(this, "评论 id 为空");
			return;
		}
		RequestInfo requestInfo = ApiRequest.sendComment(productId, content);
		getCacheManager().register(REQUEST_ID_SEND_COMMENT, requestInfo, this);
	}
}
